using System;
using System.Linq;

namespace Kaleida.ServiceMonitor.Operations.PreparedRequests
{
	public class PingReplyUnexpectedBufferException : Exception
	{
		private readonly byte[] sendBuffer;
		private readonly byte[] replyBuffer;

		public PingReplyUnexpectedBufferException(byte[] sendBuffer, byte[] replyBuffer)
			: base(BuildMessage(sendBuffer, replyBuffer))
		{
			this.sendBuffer = sendBuffer;
			this.replyBuffer = replyBuffer;
		}

		public byte[] SendBuffer
		{
			get { return sendBuffer; }
		}

		public byte[] ReplyBuffer
		{
			get { return replyBuffer; }
		}

		private static string BuildMessage(byte[] sendBuffer, byte[] replyBuffer)
		{
			return string.Format("Ping response buffer did not match send buffer.\r\nSend buffer: {0}\r\nResponse Buffer: {1}", ByteArrayToHexString(sendBuffer), ByteArrayToHexString(replyBuffer));
		}

		internal static string ByteArrayToHexString(byte[] bytes)
		{
			return string.Join(" ", bytes.Select(i=>string.Format("{0:x2}", (object) i)));
		}
	}
}